//
//  JHSettingController.m
//  Job
//
//  Created by 桑赐相 on 2018/8/26.
//  Copyright © 2018年 桑赐相. All rights reserved.
//

#import "JHSettingController.h"
#import "TableViewModel.h"
#import "UpdatePasswordController.h"
#import "MeViewController.h"
#import "JPushManager.h"
@interface JHSettingController ()

@end

@implementation JHSettingController
-(instancetype)initWithStyle:(UITableViewStyle)style{
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"系统设置";
    self.tableView.mj_header = nil;
    self.tableView.mj_footer = nil;
    [self.dataSource addObjectsFromArray:[TableViewModel settingTableViewData]];
    
    
}

- (void)exitLogging {
    [self openAlertControllerTitle:@"退出登录" message:@"确认退出？" style:UIAlertControllerStyleAlert actionTitle1:@"确认" actionTitle2:@"取消" callback:^(int index) {
        if (index == 1) {
            [[JPushManager manager] clearPush];
            [UserModel removeUserInfo];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
    }];
}


#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.font = Font16;
        cell.textLabel.textColor = HexRGB(0x444444);
        cell.detailTextLabel.font = Font14;
    }
    TableViewModel *model = self.dataSource[indexPath.row];
    cell.textLabel.text = model.name;
    if (indexPath.row == 1) {
        cell.detailTextLabel.text = [CacheManager cachSize];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row == 0) {
        UserModel *user = [UserModel getUserInfo];
        if (user) {
            UpdatePasswordController *update = [UpdatePasswordController new];
            [self pushViewController:update];
        }else{
            UIViewController *login = SBView(@"Login", @"LoginViewController");
            [self pushViewController:login];
        }
    } else {
        [CacheManager clearFileCallback:^(NSError *error) {
            [tableView reloadData];
        }];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10.0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 100.0;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    UIView *view = [UIView new];
    UserModel *user = [UserModel getUserInfo];
    if (user) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"退出登录" forState:UIControlStateNormal];
        button.backgroundColor = HexRGB(0xcccccc);
        [button setTitleColor:HexRGB(0x757575) forState:UIControlStateNormal];
        [button setLayerWithRadius:5 color:ClearColor width:0.0];
        [button addTarget:self action:@selector(exitLogging) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:button];
        
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(view).offset(40);
            make.left.equalTo(view).offset(40);
            make.right.equalTo(view).offset(-40);
            make.height.mas_offset(@40);
        }];
    }
    return view;
}

@end

